package com.shuma.shopmanage.shopmodule.controller;

import com.shuma.shopmanage.common.exception.ShopManageException;
import com.shuma.shopmanage.common.model.LoginUser;
import com.shuma.shopmanage.common.constant.RoleConstant;
import com.shuma.shopmanage.common.result.Result;
import com.shuma.shopmanage.common.result.ResultCodeEnum;
import com.shuma.shopmanage.model.shop.dto.*;
import com.shuma.shopmanage.model.shop.model.Goods;
import com.shuma.shopmanage.model.shop.vo.GoodsVo;
import com.shuma.shopmanage.model.shop.vo.PageVo;
import com.shuma.shopmanage.model.shop.vo.PreviewVo;
import com.shuma.shopmanage.security.SecurityContextHolder;
import com.shuma.shopmanage.security.annotation.RequireRole;
import com.shuma.shopmanage.shopmodule.service.GoodsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/shop")
@Api(tags = "商品模块")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/add")
    //管理员不能执行此操作
    @RequireRole(RoleConstant.USER)
    public Result addGoods(@RequestBody GoodsDto goodsDto){
        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        goodsDto.setCreateUserId(loginUser.getId());
        goodsDto.setManageUserId(loginUser.getId());
        GoodsVo goods = goodsService.insertGood(goodsDto);
        return Result.ok(goods);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageVo<GoodsVo>> queryPage(GoodsPageDto goodsPageDto){

        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        PageVo pageVo;
        if (RoleConstant.ADMIN.equals(loginUser.getRole())) {
            pageVo = goodsService.queryPage(goodsPageDto);
        } else {
            Long id = loginUser.getId();
            pageVo = goodsService.queryPageByUid(goodsPageDto, id);
        }
        return Result.ok(pageVo);
    }


    @ApiOperation("条件查询")
    @GetMapping("/query")
    public Result<PageVo<GoodsVo>> query(QueryDto queryDto, GoodsPageDto goodsPageDto) {
        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        PageVo pageVo;
        if (RoleConstant.ADMIN.equals(loginUser.getRole())) {
            pageVo = goodsService.queryPageCondition(queryDto, goodsPageDto);
        } else {
            Long id = loginUser.getId();
            pageVo = goodsService.queryPageConditionByUid(queryDto, goodsPageDto, id);
        }
        return Result.ok(pageVo);
    }

    //发起审批
    @PostMapping("/approve")
    @RequireRole(RoleConstant.USER)
    public Result goodsApprove(@RequestBody ApproveDto approveDto){
        goodsService.goodsApprove(approveDto);
        return Result.ok();
    }
    //审批批准
    @PostMapping("/approveAfter")
    @RequireRole(RoleConstant.ADMIN)
    public Result goodsApproveAfter(@RequestBody ApproveDto approveDto){
        goodsService.goodsApproveAfter(approveDto);
        return Result.ok();
    }

    @PostMapping("/batchesOffline")
    public Result goodsBatchesOffline(@RequestBody ApproveDto approveDto){
        goodsService.goodsBathesOffline(approveDto);
        return Result.ok();
    }

    @PostMapping("/Offline/{id}")
    public Result goodsOffline(@PathVariable Long id){
        goodsService.goodsOffline(id);
        return Result.ok();
    }

    @PostMapping("/batchesOnline")
    public Result goodsBatchesOnline(@RequestBody ApproveDto approveDto){
         goodsService.goodsBathesOnline(approveDto);
        return Result.ok();
    }

    @PostMapping("/Online/{id}")
    public Result goodsOnline(@PathVariable("id") Long id){
        goodsService.goodsOnline(id);
        return Result.ok();
    }

    //商品详情
    @ApiOperation("商品详情")
    @PostMapping("/goodsDetail")
    public Result getGoodsDetailByUid( Long goodsId){
        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        GoodsVo goodsVo = goodsService.getGoodsDetailById(goodsId,loginUser.getId());
        return Result.ok(goodsVo);
    }

    //商品预览
    @ApiOperation("商品预览")
    @PostMapping ("/goodsPreview")
    @RequireRole(RoleConstant.USER)
    public Result getGoodsPreviewById(Long goodsId){
        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        PreviewVo previewVo = goodsService.getGoodsPreviewById(goodsId,loginUser.getId());
        return Result.ok(previewVo);
    }


    //商品编辑
    @ApiOperation("商品编辑")
    @PostMapping("/goodsEditing")
    @RequireRole(RoleConstant.USER)
    public Result goodsEditingById(@ApiParam("商品编号") Long goodsId){
        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        Goods goods = goodsService.goodsEditingById(goodsId,loginUser.getId());
        return Result.ok(goods);
    }

    //转交管理人
    @ApiOperation("转交管理人")
    @PostMapping("/transferManagement")
    @RequireRole(RoleConstant.USER)
    public Result goodsTransferManagement(@ApiParam("商品编号") Long goodsId,@ApiParam("商户名") String userName){
        goodsService.goodsTransferManagement(goodsId,userName);
        return Result.ok();
    }

    //检验是否需要下架
    @PostMapping("/autoTakeoff")
    public Result autoTakeoff(@RequestBody ApproveDto approveDto){
        goodsService.autoTakeoff(approveDto);
        return Result.ok();
    }
}